package com.xyazm.coupon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xyazm.common.utils.ParamCheckUtil;
import com.xyazm.coupon.dao.CouponResourceDAO;
import com.xyazm.coupon.dao.UserCouponDAO;
import com.xyazm.coupon.domain.dto.UserCouponDTO;
import com.xyazm.coupon.domain.param.QueryUserCouponParam;
import com.xyazm.coupon.domain.param.ReleaseUserCouponParam;
import com.xyazm.coupon.domain.param.UseUserCouponParam;
import com.xyazm.coupon.entity.CouponResourceDO;
import com.xyazm.coupon.entity.UserCouponDO;
import com.xyazm.coupon.enums.CouponStatusEnum;
import com.xyazm.coupon.exception.CouponBizException;
import com.xyazm.coupon.exception.CouponErrorCodeEnum;
import com.xyazm.coupon.service.IUserCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Slf4j
@Service
public class UserCouponServiceImpl implements IUserCouponService {

    @Autowired
    private UserCouponDAO userCouponDAO;
    @Autowired
    private CouponResourceDAO couponResourceDAO;

    @Override
    public UserCouponDTO getUserCoupon(QueryUserCouponParam param) {
        // 入参检查
        String userId = param.getUserId();
        String couponId = param.getCouponId();
        ParamCheckUtil.checkStringNonEmpty(userId);
        ParamCheckUtil.checkStringNonEmpty(couponId);
        // 判断用户优惠券是否存在
        UserCouponDO userCouponDO = userCouponDAO.getUserCoupon(userId, couponId);
        if(userCouponDO == null) {
            throw new CouponBizException(CouponErrorCodeEnum.USER_COUPON_IS_NULL);
        }
        // 判断优惠券资源配置信息是否存在
        String couponResourceId = userCouponDO.getCouponResourceId();
        CouponResourceDO couponResourceDO = couponResourceDAO.getCouponResource(couponResourceId);
        if(couponResourceDO == null) {
            throw new CouponBizException(CouponErrorCodeEnum.USER_COUPON_CONFIG_IS_NULL);
        }
        UserCouponDTO userCouponDTO = new UserCouponDTO();
        userCouponDTO.setUserId(userId);
        userCouponDTO.setCouponResourceId(couponResourceId);
        userCouponDTO.setCouponId(couponId);
        userCouponDTO.setName(couponResourceDO.getName());
        userCouponDTO.setType(couponResourceDO.getType());
        userCouponDTO.setAmount(couponResourceDO.getAmount());
        userCouponDTO.setConditionAmount(couponResourceDO.getConditionAmount());
        userCouponDTO.setValidStartTime(couponResourceDO.getValidStartTime());
        userCouponDTO.setValidEndTime(couponResourceDO.getValidEndTime());
        // TODO 4-已过期 状态 根据过期时间来动态判断
        userCouponDTO.setCouponStatus(userCouponDO.getCouponStatus());
        return userCouponDTO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void useUserCoupon(UseUserCouponParam param) {
        log.info("lockUserCoupon->request={}", JSONObject.toJSONString(param));

        // 入参检查
        String userId = param.getUserId();
        String couponId = param.getCouponId();
        ParamCheckUtil.checkStringNonEmpty(userId);
        ParamCheckUtil.checkStringNonEmpty(couponId);
        // 判断用户优惠券是否存在
        UserCouponDO userCoupon = userCouponDAO.getUserCoupon(userId, couponId);
        if(userCoupon == null) {
            throw new CouponBizException(CouponErrorCodeEnum.USER_COUPON_IS_NULL);
        }
        // 判断优惠券是否已经使用了
        if (CouponStatusEnum.USED.getCode().equals(userCoupon.getCouponStatus())) {
            throw new CouponBizException(CouponErrorCodeEnum.USER_COUPON_IS_USED);
        }
        userCoupon.setCouponStatus(CouponStatusEnum.USED.getCode());
        userCoupon.setUsedTime(new Date());
        userCouponDAO.updateById(userCoupon);
    }

    @Override
    public void releaseUserCoupon(ReleaseUserCouponParam param) {
        // 入参检查
        String userId = param.getUserId();
        String couponId = param.getCouponId();

        log.info("开始执行释放优惠券,couponId:{}", couponId);

        ParamCheckUtil.checkStringNonEmpty(userId);
        ParamCheckUtil.checkStringNonEmpty(couponId);
        // 判断用户优惠券是否存在
        UserCouponDO userCoupon = userCouponDAO.getUserCoupon(userId, couponId);
        if(userCoupon == null) {
            throw new CouponBizException(CouponErrorCodeEnum.USER_COUPON_IS_NULL);
        }
        // 判断优惠券是否已经为已使用
        if (!CouponStatusEnum.USED.getCode().equals(userCoupon.getCouponStatus())) {
            log.info("当前用户未使用优惠券,不用回退,userId:{},couponId:{}", userId, couponId);
            return;
        }
        // 更新状态为未使用
        userCoupon.setCouponStatus(CouponStatusEnum.UN_USED.getCode());
        userCoupon.setUsedTime(null);
        userCouponDAO.updateById(userCoupon);
    }
}
